﻿using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.Linq;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.MotherHybridizationCommands
{
    public class MotherBachSearchHander : IRequestHandler<MotherBatchSearchCommand, ResultT<MotherHybidizationListDto>>
    {
        private readonly IBaseRepository<SleepInfor> baseRepository;
        private readonly ILogger<MotherBachSearchHander> logger;

        public MotherBachSearchHander(IBaseRepository<SleepInfor> repository, ILogger<MotherBachSearchHander> logger)
        {
            baseRepository = repository;
            this.logger = logger;
        }

        /// <summary>
        /// 查询配种种母信息方法
        /// </summary>
        /// <param name="request">查询配种种母信息方法请求对象</param>
        /// <param name="cancellationToken">支持异步</param>
        /// <returns>查询配种种母信息方法响应对象</returns>
        public async Task<ResultT<MotherHybidizationListDto>> Handle(MotherBatchSearchCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var result = new ResultT<MotherHybidizationListDto>();
                var list = baseRepository.GetAll().Where(c => c.SleepTypeId == 2&&c.BreedStatuId<6&& c.BreedStatuId>=1).ToList();
                if (request.VarietyId != 0)
                {
                    list = list.Where(c => c.VarietyId == request.VarietyId).ToList();
                }
                if(!string.IsNullOrEmpty( request.BreedStatuIds))
                {
                    list = list.Where(c => request.BreedStatuIds.Contains(c.BreedStatuId.ToString())).ToList();
                }
                
                if (request.StartMoonAge!=0)
                {
                    list = list.Where(c => Convert.ToDecimal((DateTime.Now - Convert.ToDateTime(c.SleepBirthDate)).TotalDays / 30) >= request.StartMoonAge).ToList();
                }
                if (request.EndMoonAge != 0)
                {
                    list = list.Where(c => Convert.ToDecimal((DateTime.Now - Convert.ToDateTime(c.SleepBirthDate)).TotalDays / 30) <= request.EndMoonAge).ToList();
                }

                if(request.RecordcottageId!=0)
                {
                    list = list.Where(c => c.RecordcottageId == request.RecordcottageId).ToList();
                }
                if (request.FieIdId != 0)
                {
                    list = list.Where(c => c.FieIdId == request.FieIdId).ToList();
                }
                result.Data =  list.Select(c=>new MotherHybidizationListDto
                {
                    SleepInforId=c.SleepInforId,
                    EarNumber=c.EarNumber,
                    VarietyId=c.VarietyId,
                    ElectronicEarTag=c.ElectronicEarTag,
                    GradeId=c.GradeId,
                    SheepImage=c.SheepImage,
                    Gender=c.Gender,
                    FarmId=c.FarmId,
                    RecordcottageId=c.RecordcottageId,
                    FieIdId=c.FieIdId,
                    FarEarNumber=c.FarEarNumber,
                    MoEarNumber=c.MoEarNumber,
                    SleepBirthDate=c.SleepBirthDate,
                    SleepBirthPlace=c.SleepBirthPlace,
                    SleepBirthWeight=c.SleepBirthWeight,
                    SleepNotes=c.SleepNotes,
                    BirthdayId=c.BirthdayId,
                    BreedStatuId=c.BreedStatuId,
                    BreedDayNum=c.BreedDayNum,
                    CheckPre = c.CheckPre,
                    ProductLevel = c.ProductLevel,
                    BuyLevel = c.BuyLevel,
                    WeanData=c.WeanData,
                    WeanWeight=c.WeanWeight,
                    AgeDay=c.AgeDay,
                    AgeMonth = Convert.ToDecimal( (DateTime.Now - Convert.ToDateTime(c.SleepBirthDate)).TotalDays / 30)
                }).ToList();
               
                logger.LogInformation("获取配种种母信息方法调用成功");
                return result;

            }
            catch (Exception ex)
            {
                logger.LogError("获取配种种母信息方法调用出错" + ex);
                throw;
            }
        }
    }
}
